package com.oracle.search.listener.service;

import com.alibaba.fastjson.JSONObject;
import com.oracle.client.ESClientConfig;
import com.oracle.index.NewsIndex;
import com.oracle.pojo.News;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class SearchService {

    @Autowired
    private ESClientConfig eSClientConfig;

    /**
     * 索引数据
     */
    public void indexNews(News news){
        //创建索引对象
        IndexRequest indexRequest=new IndexRequest("news");
        indexRequest.id(news.getId()+"");
        NewsIndex newsIndex = new NewsIndex();
        BeanUtils.copyProperties(news,newsIndex);
        newsIndex.setId(news.getId()+"");
        String json=JSONObject.toJSONString(newsIndex);
        System.out.println("service-------JSON----------->"+json);
        indexRequest.source(json, XContentType.JSON);
        try {
            IndexResponse indexResponse=this.eSClientConfig.esRestClient().index(indexRequest,this.eSClientConfig.COMMON_OPTIONS);
            System.out.println(indexResponse);
        } catch (IOException e) {
            e.printStackTrace();
        }




    }

}
